<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
class Category extends Model
{
    use SoftDeletes;
    protected $table="category";

    protected $primaryKey = 'cat_id';

    protected $fillable = ['cat_name','pid','identify','display_order'];

    protected $dates = ['deleted_at'];


    /**
     * 根据子分类获取父分类
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parentCate()
    {
        return $this->belongsTo('App\Models\Category', 'pid', 'cat_id');
    }

    /**
     * 获取某个父分类下的所有子分类
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function childrenCates()
    {
        return $this->hasMany('App\Models\Category', 'pid', 'cat_id');
    }
        //获取某个父分类下的所有子分类并分类排序
    public function indexOrderby()
    {
        return $this->hasMany('App\Models\Category', 'pid', 'cat_id')->orderBy('is_index')->where('is_index','>','0');
    }

    /**
     * 根据父分类获取所有子分类的ID
     */
    public static  function getCategoryIds($parentid)
    {
        DB::connection()->setFetchMode(\PDO::FETCH_ASSOC);
        $result = DB::table("category")->where('pid', $parentid)->lists('cat_id');

        $result = array_add($result,count($result),$parentid);
        return $result;
    }

   /* 获取他爹的id*/
    public function getFatherIds(){
        return $this->hasOne('App\Models\Category','cat_id','pid');
    }


}

